package code.structure.manager.assist
{
	import com.patience.mvc.context.Command;
	
	import flash.external.ExternalInterface;
	import flash.utils.Dictionary;
	
	import code.main.AppData;
	import code.structure.module.task.data.TaskSubData;
	
	/**
	 * 任务触发数据管理器 
	 * @author Yuri
	 * 
	 */
	public class TaskTriggerDataManager //implements ISpecialManager
	{
		private var _controller:*;
		/**
		 *当前可被触发的任务类型字典
		 */
		private var _triggerSubTaskTypeDic:Dictionary;
		
		public function TaskTriggerDataManager(c:*)
		{
			controller = c;
			_triggerSubTaskTypeDic = new Dictionary();
			if (ExternalInterface.available)
				ExternalInterface.addCallback("updataTaskRequest", updataTaskFromJS);
		}
		
		/**
		 * JS触发更新任务
		 * @param	type 任务类型
		 * @param	id   需求物品ID
		 * @param	num  数量
		 */
		public function updataTaskFromJS(type:int, id:int, num:int = 1):void {
			analyActionTriggerTask(type, id, num, true);
		}
		/**
		 *添加子任务触发类型 
		 */
		public function addSubTaskDataTrigger(subTaskList:Vector.<TaskSubData>):void {
			for (var i:int = 0; i < subTaskList.length; i += 1 ) {
				var type:int = subTaskList[i].configData.mission_require_type;
				if (!_triggerSubTaskTypeDic[type]) _triggerSubTaskTypeDic[type] = [];
				if (subTaskList[i].isSkip) continue; //如果用户已经付费完成该任务 就不加入检测列表
				_triggerSubTaskTypeDic[type].push(subTaskList[i]);
			}
		}
		/**
		 * 移除子任务触发类型
		 */
		public function removeSubTaskDataTrigger(subTaskList:Vector.<TaskSubData>):void {
			for (var i:int = 0; i < subTaskList.length; i += 1 ) {
				skipSubTask(subTaskList[i], false);
			}
		}
		/**
		 * 付费跳过单个子任务(从检测列表移除)
		 */
		public function skipSubTask(value:TaskSubData,dispatch:Boolean = true):int {
			var num:int;
			var type:int = value.config.type;
			var arr:Array =  _triggerSubTaskTypeDic[type] as Array;
			if (arr) {
				var index:int = arr.indexOf(value);
				if (index > -1) arr[index] = null;
			}
			if(dispatch){
				num = value.num;
				value.num = value.config.requireNum;
				value.isSkip = true;
				var cmd:Command = new Command("CommandNameConst.COMMAND_UPDATE_TASK + value.parent.configData.missionType, value.parent");
				cmd.data = value;
				controller.dispatch(cmd);
			}
			return num;
		}
		/**
		 *分析操作行为是否触发任务 
		 * @param type 任务类型
		 * @param targetId 目标ID
		 * @param targetNum 数量
		 * @param isAdd true:添加|false:赋值
		 */
		public function analyActionTriggerTask(type:int, targetId:* = 0, targetNum:int = 0, isAdd:Boolean = true):Boolean {
			if (isAdd && AppData.isNew) return false;
			if (_triggerSubTaskTypeDic[type] == null) return false;
			var arr:Array = (_triggerSubTaskTypeDic[type] as Array)
			var subItem:TaskSubData;
			for (var i:int = 0; i < arr.length; i += 1 ) {
				subItem = arr[i];
				//设置为空是为了防止同一个大任务里有两个子任务类型是一样的 并且有一个已完成 当完成一个时可能触发两遍任务完成  如果删除会导到数组位移 有的子任务没检测到
				if(subItem == null) continue;
				var subState:Boolean = subItem.isComplete;
				subItem.addDoNum(targetId, targetNum, isAdd);
				if(subState && subItem.isComplete) continue; //如果之前已完成 现在还是完成 跳过
				if (subItem.isComplete) {
					subItem.parent.checkTaskProgress();
				}else{
					subItem.parent.isComplete = false;
				}
				//controller.taskModel.updateTaskPostPHP(subItem.parent.id, type, targetNum); //特殊任务通知后台更新
				var cmd:Command = new Command("CommandNameConst.COMMAND_UPDATE_TASK + subItem.parent.configData.missionType, subItem.parent");
				cmd.data = subItem;
				controller.dispatch(cmd);
			}
			return false;
		}
		

		public function get controller():*
		{
			return _controller;
		}

		public function set controller(value:*):void
		{
			_controller = value;
		}

	}
}